package com.strato.webmain.servlet;

import java.lang.ref.WeakReference;

public class TimedWeakReference<T> {

	private WeakReference<T> ref;

	private long cacheTime;
	
	private int timeToLive;
	
	private int expectRefreshTime=500;
	
	public TimedWeakReference(T object) {
		this(object,500);
	}
	public TimedWeakReference(T object, int timeToLive) {
		super();
		this.ref = new WeakReference<T>(object);
		this.timeToLive = timeToLive;
		this.cacheTime=System.currentTimeMillis();
	}
	public T getObject() {
		if(ref==null){
			return null;
		}
		return ref.get();
	}
	public long getCacheTime() {
		return cacheTime;
	}
	
	public void setCacheTime(long cacheTime) {
		this.cacheTime = cacheTime;
	}
	public boolean isExpired(){
		return System.currentTimeMillis()>(cacheTime+timeToLive);
	}
	public WeakReference<T> getRef(){
		return ref;
	}
	public int getExpectRefreshTime() {
		return expectRefreshTime;
	}
	public void setExpectRefreshTime(int expectRefreshTime) {
		this.expectRefreshTime = expectRefreshTime;
	}
}
